#
# /*
#  *  Copyright 2021
#  *
#  *  This program is free software: you can redistribute it and/or modify
#  *  it under the terms of the GNU General Public License as published by
#  *  the Free Software Foundation, either version 3 of the License, or
#  *  (at your option) any later version.
#  *
#  *  This program is distributed in the hope that it will be useful,
#  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  *  GNU General Public License for more details.
#  *
#  *  You should have received a copy of the GNU General Public License
#  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  *
#  * SPDX-License-Identifier: GPL-3.0+
#  * License-Filename: LICENSE
#  *
#  */
#
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# Macro: AC_INIT (package, version, [bug-report], [tarname], [url])
# minor version number is increased at important feature change
# Process with autogen.sh or manual:
# aclocal
# autoheader
# autoconf
# libtoolize --force --automake
# automake -a --gnu --include-deps
# or do this all using autoreconf
#
# test also using scan-build from llvm clang tools
#

AC_PREREQ([2.69])
AC_INIT([ilona], [1.0], [mooigraph@gmail.com])
AC_CONFIG_SRCDIR([src/main.h])
AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE

# Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET

# -DFORTIFY_SOURCE=2 does add glibc extra checks, see RedHat documentation
# Need c11 mode because of %llu in printf() and (long long int) datatype and unnamed unions
# Upcoming c standard is c17 using GNU GCC -std=c17
# or CFLAGS="$CFLAGS ..."
# do not optimize away memset()
# -fdump-rtl-all-graph
CFLAGS="$CFLAGS -g -std=c99 -fno-builtin-memset -DFORTIFY_SOURCE=2"

# Gcc compiler options
# the option -frecord-gcc-switches puts the compiler options
# in the binary ro read with: readelf -p .GCC.command.line a.out
# https://github.com/Barro/compiler-warnings has lists of gcc options
AC_DEFUN([GCC_COMPILER_WARNING],
[AC_MSG_CHECKING(whether compiler accepts $1)
AC_SUBST(WARNING_CFLAGS)
ac_save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $1"
AC_TRY_COMPILE(,
[int x;],
WARNING_CFLAGS="$WARNING_CFLAGS $1"
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
CFLAGS="$ac_save_CFLAGS"])

# the fortify -D option needs optimizing, cannot use -O0 then.
# for debug with gdb the -O0 is needed or it cannot find crashes.
# prints gcc settings and include files and is very verbose
# GCC_COMPILER_WARNING(--verbose)
# GCC_COMPILER_WARNING(-H)
AC_ARG_ENABLE(gcc-warnings,
              AC_HELP_STRING([--enable-gcc-warnings],
	                     [turn on lots of GCC warnings (not recommended)]),
[case "${enableval}" in
   yes|no) ;;
   *)      AC_MSG_ERROR([unknown value ${enableval} for gcc-warnings option expected yes or no]) ;;
 esac],
              [enableval=no])
if test "${enableval}" = yes; then
  GCC_COMPILER_WARNING(-Werror)
  AC_SUBST([WERROR_CFLAGS], [$WARNING_CFLAGS])
  WARNING_CFLAGS=
  GCC_COMPILER_WARNING(-O0)
  GCC_COMPILER_WARNING(-Wall)
  GCC_COMPILER_WARNING(-Wextra)
  GCC_COMPILER_WARNING(-W)
  GCC_COMPILER_WARNING(-Wbad-function-cast)
  GCC_COMPILER_WARNING(-Wcast-align)
  GCC_COMPILER_WARNING(-ftrapv)
  GCC_COMPILER_WARNING(-Wformat)
  GCC_COMPILER_WARNING(-Wmissing-declarations)
  GCC_COMPILER_WARNING(-Wmissing-prototypes)
  GCC_COMPILER_WARNING(-Wnested-externs)
  GCC_COMPILER_WARNING(-Wshadow)
  GCC_COMPILER_WARNING(-Wstrict-prototypes)
  GCC_COMPILER_WARNING(-Woverlength-strings)
  GCC_COMPILER_WARNING(-Wformat-nonliteral)
  GCC_COMPILER_WARNING(-Wformat-security)
  GCC_COMPILER_WARNING(-Winit-self)
  GCC_COMPILER_WARNING(-Winline)
  GCC_COMPILER_WARNING(-Wmisleading-indentation)
  GCC_COMPILER_WARNING(-Wlogical-op)
  GCC_COMPILER_WARNING(-Wduplicated-cond)
  GCC_COMPILER_WARNING(-Wtautological-compare)
  GCC_COMPILER_WARNING(-Wshift-count-negative)
  GCC_COMPILER_WARNING(-Wshift-count-overflow)
  GCC_COMPILER_WARNING(-Wshift-negative-value)
  GCC_COMPILER_WARNING(-Wshift-overflow)
  GCC_COMPILER_WARNING(-Wnull-dereference)
  GCC_COMPILER_WARNING(-Wnonnull)
  GCC_COMPILER_WARNING(-Woverride-init-side-effects)
  GCC_COMPILER_WARNING(-Wunused-const-variable)
  GCC_COMPILER_WARNING(-Wframe-address)
else
  WARNING_CFLAGS=
  GCC_COMPILER_WARNING(-Wall)
  GCC_COMPILER_WARNING(-Wshadow)
  GCC_COMPILER_WARNING(-Wno-overlength-strings)
  GCC_COMPILER_WARNING(-pedantic)
  GCC_COMPILER_WARNING(-D_FORTIFY_SOURCE=2)
  GCC_COMPILER_WARNING(-fstack-protector-strong)
  GCC_COMPILER_WARNING(-Wl,-O1)
  GCC_COMPILER_WARNING(-frecord-gcc-switches)
  GCC_COMPILER_WARNING(-grecord-gcc-switches)
  GCC_COMPILER_WARNING(-Wpointer-arith)
  GCC_COMPILER_WARNING(-fvar-tracking-assignments)
  GCC_COMPILER_WARNING(-fcompare-debug)
  GCC_COMPILER_WARNING(-flto)
  GCC_COMPILER_WARNING(-fcallgraph-info)
  GCC_COMPILER_WARNING(-fwhole-program)
fi

# GCC compiler rtl and tree data generation
#  GCC_COMPILER_WARNING(-fdump-rtl-all-graph)
#  GCC_COMPILER_WARNING(-fdump-tree-all-graph)
#  GCC_COMPILER_WARNING(-fcallgraph-info)


# Checks for libraries.
AC_CHECK_LIB([m], [sqrt])
AC_CHECK_LIB([z], [zlibVersion])

# Checks for header files.
AC_CHECK_HEADERS([limits.h stdlib.h stdint.h string.h inttypes.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CHECK_FUNCS(memset strdup)

# final compiler settings
AC_SUBST(PACKAGE_CFLAGS)

# final compiler settings
AC_SUBST(PACKAGE_CFLAGS)
AC_SUBST(WARNING_CFLAGS)

AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT
